﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System.Data.Common;
using VisionCloud.Utility;
using VisionCloud.Utility.Security;

namespace VisionCloud.Core
{
    public class BlueDbContextFactory : IDesignTimeDbContextFactory<BlueDbContext>
    {
        internal static string ConnString { get; set; } = AesCoreHelper.Decrypt(ConfigHelper.GetValue<string>("PgsqlSetting:ConnectionMaster"), AesCoreHelper.Sceret);

        /// <summary>
        /// 获取数据库连接上下文
        /// </summary>
        /// <returns></returns>
        internal static DbConnection GetDbConnection()
        {
            return new Npgsql.NpgsqlConnection(ConnString);
        }

        public BlueDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<BlueDbContext>();
            optionsBuilder.UseNpgsql(ConnString);

            return new BlueDbContext(optionsBuilder.Options);
        }
    }
}